<template>
  <div class="login">
    <!-- <p>这是登录组件~~</p> -->
    <div class="loginbox">
      <!-- 表头 -->
      <div class="avatar_box">
        <img src="@/assets/img/logo.png" />
      </div>
      <div class="loginForm">
        <div class="titleItem">
          <h3>Login</h3>
        </div>
        <el-form
          ref="formDataRef"
          :rules="rules"
          :model="loginForm"
          label-width="80px"
        >
          <el-form-item label="用户名：" prop="username">
            <el-input
              v-model="loginForm.username"
              clearable
              prefix-icon="el-icon-user-solid"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input
              type="password"
              show-password
              v-model="loginForm.password"
              prefix-icon="el-icon-s-goods"
              @keyup.enter.native="loginInfo()"
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="loginbtn">
          <el-button type="primary" @click="loginInfo()">登录</el-button>
          <el-button type="info">注册</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>  
import {loginInfo} from '@/api/index.js';

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
    }
  },
  methods: {
    loginInfo() {
      //  this.$refs.formDataRef  表单引用对象
      this.$refs.formDataRef.validate(async (valid) => {
        // 表单验证结果
        if (!valid) {
          return
        }
        // let { data: res } = await this.$http.post('', this.loginForm)
        let res =  await loginInfo(this.loginForm);
        console.log(res);
        if (res.meta.status != 200) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success(res.meta.msg)
        sessionStorage.setItem('token', res.data.token)
        sessionStorage.setItem('userInfo',JSON.stringify(res.data))
        this.$router.push('/home')
      })
    },
  },
}
</script>

/*
    使用less需要加入less开发依赖和less-loader开发依赖
*/
<style lang="less" scoped>
.login {
  height: 100%;
  background-color: #2b4b6b;
}
.loginbox {
  width: 500px;
  height: 300px;
  background-color: white;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    border: 1px solid #eee;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .titleItem {
    position: absolute;
    top: -18%;
    left: 50%;
    transform: translate(-50%, 0);
    font-size: 20px;
  }
  .loginForm {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 5% 30px;
    box-sizing: border-box;
  }
  .loginbtn {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
